import { layer } from '@layui/layer-vue'

export default function useLayer() {
  const layerId = ref()
  const opSource = {
    type: 1,
    title: '',
    maxmin: true,
    resize: true,
    shade: false,
    moveOut: true,
    teleport: '.lf',
    area: ['800px', '580px'],
    layerClasses: 'layerBox',
    titleStyle: 'font-weight: bold'
  }
  const openLayer = (el, props = {}, fun = null, endFun = null) => {
    const params = props?.params ?? false
    if ('params' in props) delete props.params
    layer.open({
      ...opSource,
      ...props,
      success: (index) => {
        layerId.value = index
      },
      end: () => {
        layerId.value = null
        if (endFun) endFun()
      },
      content: h(el, {
        params,
        onClose(flag = 1) {
          if (flag != 10) {
            layer.close(layerId.value)
          }
          if (fun && (flag === 1 || flag === 10)) fun()
        }
      })
    })
  }
  return {
    openLayer
  }
}
